/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package myservletpackage;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import myModel.purchase;
import myModel.purchaseLogic;
import myModel.reservation;
import myModel.reservationLogic;
import myModel.userlogic;
import myModel.users;

/**
 *
 * @author RaheelArif
 */
public class mycontroller extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, ClassNotFoundException, SQLException {
        response.setContentType("text/html;charset=UTF-8");
        HttpSession session = request.getSession();

        String act = request.getParameter("action");
        if (act.equals("login")) {
            String cardnumber = null;
            String pin = null;

            cardnumber = request.getParameter("cardnumber");
            pin = request.getParameter("pin");
            //System.out.println(cardnumber);
            //System.out.println(pin);

            PrintWriter out = response.getWriter();
            userlogic userlogicobj = new userlogic();
            users userobj = new users();
            userobj.setCreditCardNumber(cardnumber);
            userobj.setPin(pin);
            String type=userlogicobj.ValidateUser(cardnumber, pin);
            System.out.println(type);
            if(type.equals("admin"))
            {
                String s = userobj.returnName();
                session.setAttribute("username", s);
                response.sendRedirect("admin_index.jsp");
            }
            else if (type.equals("user"))
            {
                String s = userobj.returnName();
                session.setAttribute("username", s);
                session.setAttribute("card",cardnumber);
                response.sendRedirect("user_index.jsp");
            }
             else
            {
                session.setAttribute("Error", "Invalid CreditCardNumber or PIN");
                session.setAttribute("card",cardnumber);
                response.sendRedirect("login.jsp");
             }


            try {
                out.println(cardnumber);
                out.println(pin);
                /* TODO output your page here
                out.println("<html>");
                out.println("<head>");
                out.println("<title>Servlet mycontroller</title>");
                out.println("</head>");
                out.println("<body>");
                out.println("<h1>Servlet mycontroller at " + request.getContextPath () + "</h1>");
                out.println("</body>");
                out.println("</html>");
                 */
            } finally {
                out.close();
            }
        }
        if(act.equals("reserve"))
        {
            System.out.println("I am in reserve");
            String SEATS= (String)session.getAttribute("seats");
            System.out.println("Seats are");
            String SID = (String)session.getAttribute("showID");
            System.out.println("yoo");
            String crd = (String) session.getAttribute("card");
            for(int i=1;i<SEATS.length();i++)
            {
                String c = "";
                for(;i<SEATS.length() && SEATS.charAt(i)!='a';i++)
                {
                    c=c+SEATS.charAt(i);
                }
                reservationLogic r = new reservationLogic();
                r.makeReservation(SID, c, crd);
                c="";

            }

            response.sendRedirect("reserved.jsp");
        }
        if(act.equals("purchase"))
        {
            String SEATS= (String)session.getAttribute("seats");
            System.out.println("Seats are");
            String SID = (String)session.getAttribute("showID");
            System.out.println("yoo");
            String crd = (String) session.getAttribute("card");
            for(int i=1;i<SEATS.length();i++)
            {
                String c = "";
                for(;i<SEATS.length() && SEATS.charAt(i)!='a';i++)
                {
                    c=c+SEATS.charAt(i);
                }
                purchaseLogic p = new purchaseLogic();
                p.makePurchase(SID, c, crd);
                c="";

            }

            response.sendRedirect("purchased.jsp");

            
        }

    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(mycontroller.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(mycontroller.class.getName()).log(Level.SEVERE, null, ex);
        }
    
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
         {
        try {
            processRequest(request, response);
            // HttpSession session = request.getSession();
            /*  String t = request.getParameter("action");
            if (t.equals("login")) {
            String name = "NULL";
            String pin = "NULL";
            name = request.getParameter("cardnumber");
            pin = request.getParameter("pin");
            if(name==null || pin==null || name.length() == 0 || pin.length() == 0)
            {
            session.setAttribute("Error", "All fields are required");
            response.sendRedirect("login.jsp");
            }
            for (int i = 0; i < name.length(); i++) {
            //If we find a non-digit character we return false.
            if (!Character.isDigit(name.charAt(i)))
            {
            session.setAttribute("Error", "CreditCard Number do not contain any alpabet ITS CREDIT CARD NUMBER!!");
            response.sendRedirect("login.jsp");
            break;
            }
            }
            PrintWriter out = response.getWriter();
            users m = new users();
            m.setCreditCardNumber(name);
            m.setPin(pin);
            try {
            if (m.ValidateUser() == true) {
            String s = m.returnName();
            session.setAttribute("User", s);
            response.sendRedirect("user_index.jsp");
            } else {
            session.setAttribute("Error", "Invalid UserName or password");
            response.sendRedirect("login.jsp");
            }
            } catch (ClassNotFoundException ex) {
            Logger.getLogger(mycontroller.class.getName()).log(Level.SEVERE, null, ex);
            } catch (SQLException ex) {
            Logger.getLogger(mycontroller.class.getName()).log(Level.SEVERE, null, ex);
            }
            try {
            //          out.println(name);
            //          out.println(pin);
            /* TODO output your page here
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet mycontroller</title>");
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet mycontroller at " + request.getContextPath () + "</h1>");
            out.println("</body>");
            out.println("</html>");
             */
            /*
            } finally {
            out.close();
            }
            }
             */
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(mycontroller.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(mycontroller.class.getName()).log(Level.SEVERE, null, ex);
        }

    }


    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
